package com.woniuxy.amusementparkos.controller.xxy;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.amusementparkos.dto.lvd.TableDataDto;
import com.woniuxy.amusementparkos.entity.wyh.Balance_Change;
import com.woniuxy.amusementparkos.entity.wyh.WristStrap_Info;
import com.woniuxy.amusementparkos.entity.xxy.Good;
import com.woniuxy.amusementparkos.entity.xxy.Order;
import com.woniuxy.amusementparkos.entity.xxy.Order_Item;
import com.woniuxy.amusementparkos.entity.xxy.Store_Info;
import com.woniuxy.amusementparkos.service.wyh.Balance_ChangeService;
import com.woniuxy.amusementparkos.service.wyh.WristStrap_InfoService;
import com.woniuxy.amusementparkos.service.xxy.GoodService;
import com.woniuxy.amusementparkos.service.xxy.OrderService;
import com.woniuxy.amusementparkos.util.lzw.result.ResponseResult;

@Controller
@RequestMapping("/store/order")
public class OrderController {
	@Autowired
	GoodService goodService;
	@Autowired
	OrderService orderService;
	@Autowired
	WristStrap_InfoService wristStrap_InfoService;
	@Autowired
	Balance_ChangeService balance_ChangeService;
	
	
	//展示商品
	@RequestMapping("/selectAll")
	@ResponseBody
	public List<Good>  selectAllStore(HttpSession session) {
		int store_id =  (int) session.getAttribute("store_id");
		try {
			List<Good> goods = goodService.selectAllGoods(store_id);
			return goods;
		} catch (Exception e) {
			return null;
		}
		
	}
	//查询全部订单
		@RequestMapping("/selectAllOrders")
		@ResponseBody
		public TableDataDto<Order>  selectAllOrders(@RequestParam("page") int page,
				@RequestParam("limit") int pageSize) {
				page=(page-1)*pageSize;
			try {
				List<Order> orders = orderService.selectAllOrders(page, pageSize);
				long count = orderService.selectAllOrdersCount();
				return new TableDataDto<Order>(0, "查询成功", count, orders);
				
			} catch (Exception e) {
				return new TableDataDto<Order>(500, e.getMessage(), null, null);
			}
			
		}
	
	//根据商店id查询全部订单
	@RequestMapping("/selectAllOrdersByStoreId")
	@ResponseBody
	public TableDataDto<Order>  selectAllOrdersByStoreId(HttpSession session,@RequestParam("page") int page,
			@RequestParam("limit") int pageSize) {
			page=(page-1)*pageSize;
		int store_id = (int) session.getAttribute("store_id");
		try {
			List<Order> orders = orderService.selectOrdersByStore_id(store_id,page,pageSize);
			long count = orderService.selectOrdersCountByStore_id(store_id);
			return new TableDataDto<Order>(0, "查询成功", count, orders);
			
		} catch (Exception e) {
			return new TableDataDto<Order>(500, e.getMessage(), null, null);
		}
		
	}
	
	//查询单个订单
		@RequestMapping("/selectOneOrder")
		@ResponseBody
		public TableDataDto<Order_Item>  selectOneOrders(String order_uuid) {
			try {
				List<Order_Item> order_items = orderService.selectOrder_itemByUuid(order_uuid);
				long count = orderService.selectOrder_itemCountByUuid(order_uuid);
				return new TableDataDto<Order_Item>(0, "查询成功", count, order_items);
			} catch (Exception e) {
				return new TableDataDto<Order_Item>(500, e.getMessage(), null, null);
			}
			
			
		}
	//生成订单
		@RequestMapping("/insertOrder")
		@ResponseBody
		public ResponseResult<String> insertOrder(@RequestParam(value = "goodId[]")int[] goodId,@RequestParam(value = "count[]")int[] count,
				HttpSession session,String wriststrp_id){
			System.out.println(wriststrp_id);
			int store_id = (int) session.getAttribute("store_id");
			String order_uuid =UUID.randomUUID().toString().replace("-", "");
			Store_Info store_info = new Store_Info();
			store_info.setStore_id(store_id);
			Double total_price = getOrderPrice(goodId, count);
			WristStrap_Info wristStrap_info = new WristStrap_Info();
			wristStrap_info.setWriststrp_id(wriststrp_id);
			//创建订单
			Order order = new Order(null, order_uuid, null, store_info , total_price, wristStrap_info , null, 0);
			try {
				orderService.insertOrder(order);
				
			} catch (Exception e) {
				System.out.println("error");
			}
			//创建订单条目
			for(int i=0;i<goodId.length;i++) {
				Double subtotal = getOrderPrice(goodId[i], count[i]);
				try {
					Order order02 = orderService.selectOrderInfoByUuid(order_uuid);
					
					Good good = new Good();
					good.setGood_id(goodId[i]);
					Order_Item order_item
						= new Order_Item(null, good, count[i], order02, subtotal);
					orderService.insertOrder_item(order_item);
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				
			 }
			//扣除腕带余额
			try {
				WristStrap_Info wriststrap = wristStrap_InfoService.getWorkStateWristStrap(wriststrp_id);
				
				if(wriststrap.getBalance().doubleValue()<total_price) return new ResponseResult<String>(500, "余额不足订单支付失败", null);
				
				//执行扣除
				wristStrap_InfoService.changeBalacne(wriststrap.getInfo_id(),new BigDecimal(wriststrap.getBalance().doubleValue()-total_price) );
				//腕带信息变动
				Balance_Change balance_Change = new Balance_Change(null, wriststrap.getInfo_id(), 0-total_price, 4, null, 5, 0, 1);
				balance_ChangeService.addObject(balance_Change);
				orderService.updateOrderStateByUUID(order_uuid);
			} catch (Exception e) {
				return new ResponseResult<String>(500, e.getMessage(), null);
			}
			return new ResponseResult<String>(0, "消费成功", null);
			}
	
	//获取订单总价
		public  Double getOrderPrice(int[] goodId,int[] count) {
			double total =0;
			for(int i=0;i<goodId.length;i++) {
				try {
					Good good = goodService.selectGoodByGid(goodId[i]);
					total += good.getUnit_price()*count[i];
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				
			}
			return total;
		}	
	//获取订单条目总价
		public  Double getOrderPrice(int goodId,int count) {
			double subtotal=0;
			try {
				Good good = goodService.selectGoodByGid(goodId);
				return  subtotal=good.getUnit_price()*count; 
			} catch (Exception e) {
				System.out.println(e.getMessage());
				return null;
			}
			
		}
	
	//以下为页面跳转
	@RequestMapping("/showOrder")
	public String showOrder() {
		return "xxy/订单信息页";
	}
	
	@RequestMapping("/getOrder")
	public String getOrder() {
		return "xxy/订单生成页";
	}
	@RequestMapping("/showAllOrder")
	public String showAllOrder() {
		return "xxy/订单信息页_主页面";
	}
	
}
